/*
 * FixedSizeBlock.cpp
 *
 *  Created on: Oct 14, 2012
 *      Author: m
 */

#include "FixedSizeBlock.h"

namespace physical
{

FixedSizeBlock::FixedSizeBlock( size_t size ):m_blockSize(size), m_contentLength(0)
{
	m_content = UP_byteStream ( new byte[size] );
}

FixedSizeBlock::FixedSizeBlock( size_t size, UP_byteStream content, size_t contentLength )
{
	m_content = std::move(content);
	m_blockSize = size;
	m_contentLength = contentLength;
}

byteStream FixedSizeBlock::getContent()
{
	return m_content.get();
}

bool FixedSizeBlock::setContent( const byteStream newContent, size_t newContentLength )
{
	bool contentFitInBlock = false;
	if ( newContentLength<= m_blockSize )
	{
		memcpy(m_content.get(),newContent,newContentLength);
		m_contentLength = newContentLength;
		contentFitInBlock = true;
	}
	return contentFitInBlock;
}

bool FixedSizeBlock::appendContent( const byteStream newContent, size_t newContentLength )
{
	bool contentFitInBlock = false;
	if ( (newContentLength+m_contentLength)<= m_blockSize )
	{
		memcpy(m_content.get()+m_contentLength,newContent,newContentLength);
		m_contentLength += newContentLength;
		contentFitInBlock = true;
	}
	return contentFitInBlock;
}

size_t FixedSizeBlock::getBlockSize()
{
	return m_blockSize;
}

size_t FixedSizeBlock::getContentLength()
{
	return m_contentLength;
}

FixedSizeBlock::~FixedSizeBlock()
{
}

}
